<?php

class CBSession extends CDbHttpSession {

/**
 * Creates the session DB table.
 * @param CDbConnection the database connection
 * @param string the name of the table to be created
 */
    protected function createSessionTable($db,$tableName) {
        $sql="
CREATE TABLE $tableName
(
	id CHAR(32) NOT NULL PRIMARY KEY,
	expire INTEGER NOT NULL ,
        created INTEGER NOT NULL ,
	data TEXT,
        UNIQUE INDEX `unique_yii_session` (`id` ASC)
)";
        $db->createCommand($sql)->execute();
    }

    /**
     * Session write handler.
     * Do not call this method directly.
     * @param string session ID
     * @param string session data
     * @return boolean whether session write is successful
     */
    public function writeSession($id,$data) {
        /*INSERT INTO session (create_ts, session_key, session_data) VALUES (NOW(), :id, :data) ON DUPLICATE KEY UPDATE create_ts=NOW(), session_data=:data'*/
        $created = time();
        $expire=$created + $this->getTimeout();
        $id=md5($id);
        $db=$this->getDbConnection();

        $sql = "INSERT INTO {$this->sessionTableName} (id, data, expire, created) VALUES ('$id', :data, $expire, $created) ON DUPLICATE KEY UPDATE data=:data, expire=$expire, created=$created;";

        $command=$db->createCommand($sql);
        $command->bindParam(':data',$data);
        $command->execute();
        return true;
    }

    /**
     * @return integer Get number of online users
     */
    public function getOnline(){
        return 0;
    }

}
